
int handlePrimitive()
{
#ifdef PRINT_PRIMITIVES
	printf("<primitive: %d ( %s )>\n", primitiveIndex, symbolToCString(methodSelector(newMethod)));
#endif
	success = 1;

	switch(primitiveIndex)
	{
		case 1: primAdd(); break; //arithmeticPrimitives
		case 2: primSubtract(); break;
		case 3: primLessThan(); break;
		case 4: primGreaterThan(); break;
		case 5: primLessOrEqual(); break;
		case 6: primGreaterOrEqual(); break;
		case 7: primEqual(); break;	
		case 8: primNotEqual(); break;	
		case 9: primMultiply(); break;
		case 10: primDivide(); break;
		case 11: primMod(); break;
		case 12: primDiv(); break;
		case 13: primQuo(); break;
		case 14: primBitAnd(); break;
		case 15: primBitOr(); break;
		case 16: primBitXor(); break;
		case 17: primBitShift(); break;
		case 18: primMakePoint(); break;
		case 19: primFail(); break; //Simulation guard, must fail
		case 20: 
		case 21:
		case 22:
		case 23:
		case 24:
		case 25:
		case 26:
		case 27:
		case 28:
		case 29:
		case 30:
		case 31:
		case 32:
		case 33:
		case 34:
		case 35:
		case 36:
		case 37:
		case 38:
		case 39: primFail(); break; //LargeInteger primitives
		case 40: primAsFloat(); break; //floatPrimitives
		case 41: primFloatAdd(); break;
		case 42: primFloatSubtract(); break;
		case 43: primFloatLessThan(); break;
		case 44: primFloatGreaterThan(); break;
		case 45: primFloatLessOrEqual(); break;
		case 46: primFloatGreaterOrEqual(); break;
		case 47: primFloatEqual(); break;
		case 48: primFloatNotEqual(); break;
		case 49: primFloatMultiply(); break;
		case 50: primFloatDivide(); break;
		case 51: primTruncated(); break;
		case 52: primFractionPart(); break;
		case 53: primExponent(); break;
		case 54: primTimesTwoPower(); break;
		case 55: primSqrt(); break;
		case 56: primSin(); break;
		case 57: primAtan(); break;
		case 58: primLn(); break;
		case 59: primExp(); break;
		case 60: primAt(); break; //subscriptPrimitives
		case 61: primAtPut(); break;
		case 62: primSize(); break;
		case 63: primStringAt(); break; 
		case 64: primStringAtPut(); break;
		case 65: primNext(); break; 
		case 66: primNextPut(); break;
		case 67: primAtEnd(); break;
		case 68: primObjectAt(); break;
		case 69: primObjectAtPut(); break;
		case 70: primNew(); break; //storage prims
		case 71: primNewN(); break;
		//case 72: primArrayBecomeOneWay(); break;
		case 73: primInstVarAt(); break;
		case 74: primInstVarAtPut(); break;
		case 75: primHash(); break;
		case 76: primStoreStackp(); break;
		case 77: primSomeInstance(); break;
		case 78: primNextInstance(); break;
		case 79: primNewMethod(); break;
		case 80: primBlockCopy(); break; //control prims
		case 81: primValue(argumentCount); break;
		case 82: primValueWithArgs(argumentCount); break;
		case 83: primPerform(argumentCount); break;
		case 84: primPerformWithArgs(); break;
		case 85: primitiveSignal(); break;
		case 86: primitiveWait(); break;
		case 87: primitiveResume(); break;
		case 88: primitiveSuspend(); break;
		case 89: primNoOp(); break; //flushCache  We haven't implemented a method cache yet, so nothing to flush.
		case 90: primFail(); break; //primMousePt		//90-109 IO primitives
		case 91: primFail(); break; //supportsDisplayDepth		
		//case 92:
		case 93: primFail(); break; //set input semaphore		
		case 94: primFail(); break; //primGetNextEvent:	
		
		case 101: primNoOp(); break; //beCursor
		case 102: primNoOp(); break; //beDisplay
		case 103: primFail(); break; //basicScanCharacters...
		
		case 105: primFail(); break; //replaceFrom:to:with:startingAt: OPTIONAL
		case 106: primFail(); break; //actualScreenSize
		case 107: primFail(); break; //primMouseButtons
		case 108: primFail(); break; //primKbdNext
		case 110: primIdentical(); break; //system prims
		case 111: primClass(); break;
		case 112: popNandPush(argumentCount+1, packSmInt(1024*1024*10)); break; //primBytesLeft
		case 117: primExternalCall(); break; //aka named primitivies
		case 121: popNandPush(1, packString("/oceania.image"));break; //primImageName
		case 124: primNoOp(); break; //primLowSpaceSemaphore:
		case 125: primNoOp(); break; //primSignalAtBytesLeft
		case 126: primFail(); break; //primitiveDeferUpdates:
		case 129: popNandPush(1, specialObjectsArray); break;
		case 130: primFail(); break; //garbageCollect
		case 131: primFail(); break; //garbageCollectMost
		case 132: primFail(); break; //pointsTo:
		case 133: primNoOp(); break; //primSetInterruptKey:
		case 134: primNoOp(); break; //primInteruptSemaphore:
		case 135: popNandPush(1, packSmInt(millisecond_clock)); break; //millisecondClockValue
		case 136: primitiveSignalAtMilliseconds(); break;
		case 142: primFail(); break; //vmpath
		case 143: primSignedShortAt(); break;
		case 148: primClone(); break; //shallowCopy	BOGUS: Essential when used through Object>>clone!!!!!
		case 149: primGetAttribute(); break;
		case 161: primBecome161(); break;
		case 163: primFail(); break; //isImmutable OPTIONAL(unsupported)
		case 167: primFail(); break; //Processor yeild.  OPTIONAL.
		case 195: primFail(); break; //findNextUnwindContextUpTo:
		case 196: primFail(); break; //terminateTo:
		case 198: primFail(); break; //ensure: Just a marker.  Must fail.
		case 199: primFail(); break; //on:do:  Just a marker.  Must fail.
		
		//device primitives
		case 210: primReadByteFromPort(); break;
		case 211: primWriteByteToPort(); break;
		case 212: primReadShortFromPort(); break;
		case 213: primWriteShortToPort(); break;
		case 214: primReadLongFromPort(); break;
		case 215: primWriteLongToPort(); break;
		case 216: primReadFromAddress(); break;
		case 217: primWriteToAddress(); break;
		case 218: primReadFromAddressBounded(); break;
		case 219: primWriteToAddressBounded(); break;
		case 220: primDebugPrint(); break;
		case 221: primPanic(); break;
		case 222: primIsOceania(); break;
		//to 225 are available for our use
		
		case 230: primRelinquishProcessor(); break;
		case 254: primNoOp(); break; //vmParameterAt:put:

		case 256: popNandPush(argumentCount+1,peek(0)); break; //return self
		case 257: popNandPush(argumentCount+1,true); break; //return true
		case 258: popNandPush(argumentCount+1,false); break; //return false
		case 259: popNandPush(argumentCount+1,nil); break; //return nil
		case 260: popNandPush(argumentCount+1,packSmInt(-1)); break;
		case 261: popNandPush(argumentCount+1,packSmInt(0)); break;
		case 262: popNandPush(argumentCount+1,packSmInt(1)); break;
		case 263: popNandPush(argumentCount+1,packSmInt(2)); break;
                                        
		case 264: case 265: case 266: case 267: case 268: case 269: case 270: case 271:
		case 272: case 273: case 274: case 275: case 276: case 277: case 278: case 279:
		case 280: case 281: case 282: case 283: case 284: case 285: case 286: case 287: 
		case 288: case 289: case 290: case 291: case 292: case 293: case 294: case 295: 
		case 296: case 297: case 298: case 299: case 300: case 301: case 302: case 303: 
		//all the way to 519 ...
            popNandPush( argumentCount+1, slotOf(peek(0),primitiveIndex-263) ); //return instance variable
            break;

		default:
			printf("\n<primitive: %d ( %s )>", primitiveIndex, symbolToCString(methodSelector(newMethod)) );
			PANIC("primitive not implemented");
	}
	
	return success;
}

void primExternalCall()
{
	int array = slotOf(newMethod, 2); //first literal
	ASSERT( sizeOf(array)==4, "Named prims should have 4-tuple");
	
	int moduleName = slotOf(array, 1); //<String|Symbol>
	int functionName = slotOf(array, 2); //<String|Symbol>
	int sessionID = slotOf(array, 3);
	int functionIndex = slotOf(array, 4);
	
	char *funcName = symbolToCString(functionName);
	
	if( !strcmp(funcName,"primitiveCompareString") ) success=0; //OPTIONAL
	else if( !strcmp(funcName,"primitiveFindSubstring") ) success=0; //OPTIONAL
	else if( !strcmp(funcName,"primDigitMultiplyNegative") ) success=0; //OPTIONAL
	else if( !strcmp(funcName,"primNormalizePositive") ) success=0; //OPTIONAL
	else if( !strcmp(funcName,"primDigitCompare") ) success=0; //OPTIONAL
	else if( !strcmp(funcName,"primDigitDivNegative") ) success=0; //OPTIONAL
	else if( !strcmp(funcName,"primDigitAdd") ) success=0; //OPTIONAL
	else if( !strcmp(funcName,"primDigitSubtract") ) success=0; //OPTIONAL
	else if( !strcmp(funcName,"primitiveIndexOfAsciiInString") ) success=0; //OPTIONAL
	else if( !strcmp(funcName,"primitiveDirectoryLookup") )
	{
		popNandPush( argumentCount+1, nil );
	}
	else if( !strcmp(funcName,"primitiveDirectoryDelimitor") )
	{
		popNandPush( 1, slotOf( CharacterTable, '/'+1 ) );
	}
	else if( !strcmp(funcName,"primSizeField") )
	{
		int alien = peek(0);
		int size = packSigned32(slotOf(alien, 1));
		if(success)
		{
			popNandPush(argumentCount+1, size);
		}
	}
	else
	{
		printShortObject(moduleName);
		printShortObject(functionName);
		PANIC("Named primtive");
	}
}

void primFail()
{
	success=0;
}

void primNoOp()
{
	popNandPush(argumentCount+1, peek(argumentCount)); //leaving receiver
}
